package com.zb.controller.settings;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.common.annotation.Permission;
import com.zb.common.exception.BusinessException;
import com.zb.common.po.enums.MenuType;
import com.zb.common.po.enums.ResponseCode;
import com.zb.common.po.model.Menu;
import com.zb.common.po.vo.AjaxResponse;
import com.zb.service.MenuService;

@Controller
@RequestMapping(value = "/settings")
public class MenuController {

    Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Resource
    private MenuService menuService;

    /**
     * 角色列表 roleList:(这里用一句话描述这个方法的作用)
     * 
     * @author luohaili
     * @return
     * @since JDK 1.7
     */
    @Permission(code = "menu:manage")
    @RequestMapping(value = "/menu_settings.do")
    public String menuSettings() {
        return "page/settings/menu_settings";
    }

    /**
     * 加载菜单
     * 
     * @param session
     * @return
     */
    @Permission(code = "menu:manage")
    @ResponseBody
    @RequestMapping(value = "/load_menues.do")
    public AjaxResponse<List<Menu>> loadMenues(HttpSession session) {
        AjaxResponse<List<Menu>> response = new AjaxResponse<List<Menu>>();
        try {
            List<Menu> menuList = menuService.findAllMenu();
            response.setData(menuList);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("查询菜单异常：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("获取菜单异常");
        }
        return response;
    }

    @Permission(code = "menu:manage")
    @ResponseBody
    @RequestMapping(value = "/save_menue.do")
    public AjaxResponse<Menu> saveMenue(HttpSession session, Menu menu,
            Integer menuTypeValue) {
        AjaxResponse<Menu> response = new AjaxResponse<Menu>();
        try {
            menu.setMenuType(MenuType.getStatusByType(menuTypeValue));
            menuService.saveMenu(menu);
            response.setData(menu);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (BusinessException e) {
            logger.error("保存菜单错误：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            logger.error("保存菜单错误：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("保存菜单异常");
        }
        return response;
    }

    @Permission(code = "menu:manage")
    @ResponseBody
    @RequestMapping(value = "/del_menue.do")
    public AjaxResponse<Object> delMenue(HttpSession session, Integer id) {
        AjaxResponse<Object> response = new AjaxResponse<Object>();
        try {
            menuService.deleteMenu(id);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (BusinessException e) {
            logger.error("删除菜单失败：{}", e);
            response.setResponseCode(ResponseCode.BUSINESSERROR);
            response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            logger.error("删除菜单失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("删除菜单");
        }
        return response;
    }
}
